//删除并获得点数（medium): https://leetcode.cn/problems/delete-and-earn/
class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
        const int N = 10001;
        // 1. 预处理
        int arr[N] = {0};
        for (auto x : nums)
            arr[x] += x;
        // 2. 在 arr 数组上，做⼀次 “打家劫舍” 问题
        // 创建 dp 表
        vector<int> f(N);
        auto g = f;
        // 填表
        for (int i = 1; i < N; i++) {
            f[i] = g[i - 1] + arr[i];
            g[i] = max(f[i - 1], g[i - 1]);
        }
        // 返回结果
        return max(f[N - 1], g[N - 1]);
    }
};

//粉刷房⼦（medium）: https://leetcode.cn/problems/JEj789/
class Solution {
public:
    int minCost(vector<vector<int>>& costs) {
        // dp[i][j] 第i个房⼦刷成第j种颜⾊最⼩花费
        int n = costs.size();
        vector<vector<int>> dp(n + 1, vector<int>(3));
        for (int i = 1; i <= n; i++) {
            dp[i][0] = min(dp[i - 1][1], dp[i - 1][2]) + costs[i - 1][0];
            dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + costs[i - 1][1];
            dp[i][2] = min(dp[i - 1][1], dp[i - 1][0]) + costs[i - 1][2];
        }
        return min(dp[n][0], min(dp[n][1], dp[n][2]));
    }
};